#pragma once
#include"Logic.h"
#include<string>
#include <stdlib.h>
#include <vcclr.h>

namespace TaskNinja {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace std;


	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
		Logic* logic;

		void initialize(){
			logic = new Logic;

			//cli::pin_ptr<TaskList> showListP = showList;  
            //SomeFunction( static_cast<NativeType *>( p ) );
		}
		void cleanUp(){
			delete logic;
		}
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			initialize();
		}
	    string getUserInput(){
			 userInput=this->textBox1->Text;
			 string command;
			 command = To_string(userInput);
			 return command;
		}
		void processCommand(string command){
			logic->getCommand(command);
			logic->processCommand();
			//save after every command processed
				 logic->getAllTask().getUpcomingTasks().save("upcoming tasks.txt");
				 logic->getAllTask().getDeletedTasks().save("deleted tasks.txt");
				 logic->getAllTask().getFinishedTasks().save("finished tasks.txt");
		}
		TaskList getResultList(){
			return logic->getResultList();
		}
		void displayInListBox1(TaskList* showList){
			
			 Task* curTask=new Task;
			 this->listBox1->Items->Clear();
			 this->textBox1->Clear();
			 int maxId = showList->getLength();
			 for(int i=1;i<=maxId;i++){
				 showList->retrieve(i,*curTask);

				 string taskString=curTask->toPrint();
				 String^ taskSystemString = To_String(taskString);

				 this->listBox1->Items->Add(taskSystemString);
			 }
			 delete curTask;
		}
		void displayLabel1Text(Logic* l){
			commandType type=l->getCommandType();
			this->label1->Text = "";
			switch(type){
			case Add:
				this->label1->Text=ADD_MESSAGE;
				break;
			case Delete:
				this->label1->Text=DELETE_MESSAGE;
				break;
			case Edit:
				this->label1->Text=EDIT_MESSAGE;
				break;
			case 3://update because it is a keyword
				this->label1->Text=UPDATE_MESSAGE;
				break;
			case Undo:
				this->label1->Text=UNDO_MESSAGE;
				break;
			case Redo:
				this->label1->Text=REDO_MESSAGE;
				break;
			default:
				break;
			}
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}

			cleanUp();
		}

	private: System::Windows::Forms::ListBox^  listBox1;
	protected: 
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Button^  enterButton;

	private: System::Windows::Forms::Label^  label1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
		String^ userInput;

		static String ^ const ADD_MESSAGE="Add successful!";
        static String ^ const DELETE_MESSAGE="Delete successful!";
		static String ^ const UPDATE_MESSAGE="Update successful!";
		static String ^ const EDIT_MESSAGE="Edit successful!";
		static String ^ const UNDO_MESSAGE="Undo operation.";

		 static String ^ const REDO_MESSAGE="Redo operation.";

		string To_string(String^ source)
		{
		    pin_ptr<const wchar_t> wch = PtrToStringChars(source);
			size_t origsize = wcslen(wch) + 1;
		    const size_t newsize = 100;
		    size_t convertedChars = 0;
		    char nstring[newsize];
		    wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
			string result(nstring);
			return result;
		}

		String^ To_String(string source){
			const char* source_ch=source.c_str();
			String ^systemstring = gcnew String(source_ch);
			return systemstring;
		}

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->enterButton = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// listBox1
			// 
			this->listBox1->FormattingEnabled = true;
			this->listBox1->HorizontalScrollbar = true;
			this->listBox1->Location = System::Drawing::Point(13, 58);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(321, 173);
			this->listBox1->TabIndex = 0;
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(13, 13);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(235, 20);
			this->textBox1->TabIndex = 1;
			this->textBox1->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::textBox1_KeyDown);
			// 
			// enterButton
			// 
			this->enterButton->BackColor = System::Drawing::Color::White;
			this->enterButton->Location = System::Drawing::Point(264, 12);
			this->enterButton->Name = L"enterButton";
			this->enterButton->Size = System::Drawing::Size(70, 23);
			this->enterButton->TabIndex = 2;
			this->enterButton->Text = L"Enter";
			this->enterButton->UseVisualStyleBackColor = false;
			this->enterButton->Click += gcnew System::EventHandler(this, &Form1::enterButton_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(12, 243);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(103, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"Load file successful!";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::Color::White;
			this->ClientSize = System::Drawing::Size(362, 270);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->enterButton);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->listBox1);
			this->Name = L"Form1";
			this->Text = L"TaskNinja";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
				 logic->getAllTask().getUpcomingTasks().load("upcoming tasks.txt");
				 logic->getAllTask().getDeletedTasks().load("deleted tasks.txt");
				 logic->getAllTask().getFinishedTasks().load("finished tasks.txt");
			 }
	
private: System::Void textBox1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
			 if(e->KeyCode == Keys::Enter){
					this->enterButton->PerformClick();
			 }
		 }
private: System::Void enterButton_Click(System::Object^  sender, System::EventArgs^  e) {
			 string command=getUserInput();
			 processCommand(command);
			 TaskList* resultList=new TaskList;
			 *resultList = getResultList();
			 displayInListBox1(resultList);
			 displayLabel1Text(logic);
			 delete resultList;
		 }
};
}

